
Date.prototype.Format = function (fmt) { //author: meizz
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

// 根据url判断
setTimeout(function(){
	var url1 = window.location.href;
	if(url1.indexOf("yellowPage")>0){
		$("footer .foot01").removeClass("active01");
		$("footer .foot02").addClass("active02");
		$("footer .foot02").parent('p').addClass("redOn");
		$("footer .foot03").removeClass("active03");
		$("footer .foot04").removeClass("active04");
	}else if(url1.indexOf("bulkTrader")>0){
		$("footer .foot01").removeClass("active01");
		$("footer .foot02").removeClass("active02");
		$("footer .foot03").addClass("active03");
		$("footer .foot03").parent('p').addClass("redOn");
		$("footer .foot04").removeClass("active04");
	}else if(url1.indexOf("userCenter")>0){
		$("footer .foot01").removeClass("active01");
		$("footer .foot02").removeClass("active02");
		$("footer .foot03").removeClass("active03");
		$("footer .foot04").addClass("active04");
		$("footer .foot04").parent('p').addClass("redOn");
	}else{
		$("footer .foot01").addClass("active01");
		$("footer .foot01").parent('p').addClass("redOn");
		$("footer .foot02").removeClass("active02");
		$("footer .foot03").removeClass("active03");
		$("footer .foot04").removeClass("active04");
	}
},500);

// 获取url中的参数
function getUrlParams() {
	var resultObj = {};
	var params = window.location.search.substr(1);
	if (params.length > 0 ) {
		var kvs = params.split("&");
		for (var str in kvs) {
			var j = kvs[str].split("=");
			resultObj[j[0]] = j[1];
		}
	}
	return resultObj;
}

// 添加全局拦截器
Vue.http.interceptors.push(function(request, next) {
	this.showLoading = true
	next(function(response){
		var result = JSON.parse(response.body);
		if(result.code == 10){
			//token参数错误,清除本地缓存,页面重新加载
    	clearToken();
			promptBox(result.msg);
			window.location.reload();
			return;
		}
		if(0!=result.code){
			if(10200 == result.code){
				this.$http.get(wxUrl + "/v3.2/login/login", {params:{token: token}})
					.then(function(response) {
						var result = JSON.parse(response.body);
						if(result){
							if(result.code == 0){
								if(result.data.success==true){
									if(result.data.register==true){
										if(result.data.complete==true){
											localStorage.setItem("complete", true);
											localStorage.setItem("userToken", result.data.userToken);
											location.reload();
										}else{//完善个人信息
											promptBox('您须填写完善所有信息');
											setTimeout(function(){
												window.location.href= self_base_path + "/userCenter/personalData/personalData.html";
											}, 1000);
										}
										localStorage.setItem("userToken", result.data.userToken);
									}else{//注册手机号
										window.location.href= self_base_path + "/userCenter/paymentPassword/signIn.html";
									}
								}else{//微信授权
									var url = window.location;
									window.location.href= wxUrl + "/v3.2/login/auth?token=" + token + "&url=" + url;
								}
							}else{	
								if(result.code == 10){
									//token参数错误,清除本地缓存,页面重新加载
								   clearToken();
									window.location.reload();
								}else{
									promptBox(result.msg);
								}
							}
						}else{
							promptBox('网络异常！请检查您的网络！');
						}
					});
			}else{						
				promptBox(result.msg);							
			}
		}
		this.showLoading = false
		return response
	});
});

// 全局过滤器 —— 100.00 > 100
Vue.filter("quzheng", function(value) {
	var numArray = value.split(".");
	var num1 = numArray[0];
	var num2 = numArray[1];
	if(num2 == 00){
		return value = num1;
	}else{
		return value;
	}
});

//toast提示框
var toast_showing=false;
function promptBox(message){	
	if(toast_showing){return;}
	toast_showing=true;
	var newDiv = document.createElement("div");
	newDiv.setAttribute("id", "newD");
	newDiv.className="promptBox";
	newDiv.innerHTML=message;
	document.body.appendChild(newDiv);
	//shadow
	var shadowDiv = document.createElement("div");
	shadowDiv.setAttribute("id", "shadowId");
	shadowDiv.className="shadowClass";
	document.body.appendChild(shadowDiv);
	
	newDiv.style.display="block";
	setTimeout("removeDiv()", 1500);
	var clientWidth = document.body.clientWidth;
	var divWidth = $('#newD').width() + 30;
	var leftMuch = (clientWidth - divWidth)/2;
	$('#newD').css('left',leftMuch);
}
function removeDiv(){
	toast_showing=false;
	var promptDiv = document.getElementById('newD');
	document.body.removeChild(promptDiv);
	var shadowDiv = document.getElementById('shadowId');
	document.body.removeChild(shadowDiv);
}

//dialog确认框
function dialog(message,funA){
	var dialog = '<div id="dialog">' +
				 '<div class="tanceng">' +
				 '<p class="title">提示</p>' +
				 '<div class="textben">'+message+'</div>' +
				 '<div class="xcf">' +
				 '<div class="bot"></div>' +
				 '<div class="choose">' +
				 '<span id="cancel" class="bor">取消</span>' +
				 '<span id="sure" v-on:click="funA()">确定</span>' +
				 '</div>' +
			     '</div>' +
		         '</div>' +
		         '<div class="overlay"></div>' +
		         '</div>';
	jQuery("body").append(dialog);
	jQuery("#cancel").click(function(){
		jQuery("#dialog").remove();
	});
}

//jquery ajax get
function xget (url,param,callback,errorback){
	$.ajax({
		type:'get',
		url : url,
		data : param,
		dataType: 'json',
		success : callback,
		error : errorback
	});
}

//获取token
function getToken(){
	var localToken = localStorage.getItem('token');
	if(localToken == null || localToken == ''){
		xget(
			wxUrl + '/v3.2/login/token',
			{},
			function(data){
				token = data.data.token;
				localStorage.setItem('token',token);
			}
		);
	}else{
		token = localToken;
	}
}
//清除token
function clearToken(){
	localStorage.removeItem('token');
	localStorage.removeItem('userToken');
}

//获取userToken
function getUserToken(){
	var localUserToken = localStorage.getItem('userToken');
	if(localUserToken!= null && localUserToken != ''){
		userToken = localUserToken;
	}
}

//测试定位信息---稍后删除
//localStorage.removeItem('location');

//获取定位信息
function getLocation(){
	var location = localStorage.getItem('location');
	var result = JSON.parse(location);
	if(result == null || result == ''){
		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r){
			if(this.getStatus()== BMAP_STATUS_SUCCESS){
				longitude = r.point.lng;
				latitude = r.point.lat;
				//根据经纬度获取省市县
				var point = new BMap.Point(longitude,latitude);
				var gc = new BMap.Geocoder();
				gc.getLocation(point, function(rs){
					var addComp = rs.addressComponents;
					province = addComp.province;
					city = addComp.city;
					district = addComp.district;
					//本地存储定位信息
					var locationData = new Object;
					locationData.province = province;
					locationData.city = city;
					locationData.district = district;
					locationData.longitude = longitude;
					locationData.latitude = latitude;
					var locationStr = JSON.stringify(locationData);
					localStorage.setItem('location',locationStr);
					//首页定位显示当前市
					var cityName = document.getElementById('cityName');
					if(cityName){
						$("#cityName").text(city);
					}
				});
			}
		},{enableHighAccuracy: true});
	}else{
		province = result.province;
		city = result.city;
		district = result.district;
		longitude = result.longitude;
		latitude = result.latitude;
	}
}

// 验证登录——跳转页面
function vLoginJump(pageUrl){
	if(userToken != null && userToken != ''){
		var complete = localStorage.getItem('complete');
		if(complete){
			window.location.href= self_base_path + pageUrl;
		}else{
			vJump(pageUrl);
		}
	}else{
		vJump(pageUrl);
	}
}
function vJump(pageUrl){
	xget(
		wxUrl + "/v3.2/login/login",
		{token: token},
		function(data){
			if(data){
	        	if(data.code == 0){
	        		if(data.data.success==true){
	        			if(data.data.register==true){
	        				if(data.data.complete==true){
	        					localStorage.setItem("complete", true);
	        					localStorage.setItem("userToken", data.data.userToken);
	        					window.location.href= self_base_path + pageUrl;
	        				}else{ //完善个人信息
	        					promptBox('您须填写完善所有信息');
	        					setTimeout(function(){
	        						window.location.href= self_base_path + "/userCenter/personalData/personalData.html";
	        					}, 1000);
	        				}
	        			}else{ //绑定手机号
	        				window.location.href= self_base_path + "/userCenter/paymentPassword/signIn.html";
	        			}
	        		}else{ //微信授权
	        			var url = window.location;
	        			window.location.href= wxUrl + "/v3.2/login/auth?token=" + token + "&url=" + url;
	        		}
	        	}else{
	        		promptBox(data.msg);
	        	}
	        }else{
	        	promptBox('网络异常，请检查您的网络！');
	        }
		}
	);
}

// 验证登录——收藏关注类
function vLoginClick(){
	if(userToken != null && userToken != ''){
		var complete = localStorage.getItem('complete');
		if(complete){
			return true;
		}else{
			vClick();
		}
	}else{
		vClick();
	}
}
function vClick(){
	xget(
		wxUrl + "/v3.2/login/login",
		{token: token},
		function(data){
			if(data){
	        	if(data.code == 0){
	        		if(data.data.success==true){
	        			if(data.data.register==true){
	        				if(data.data.complete==true){
	        					userToken = data.data.userToken;
	        					localStorage.setItem("userToken", data.data.userToken);
	        					localStorage.setItem("complete", true);
	        					return true;
	        				}else{ //完善个人信息
	        					promptBox('您须填写完善所有信息');
	        					setTimeout(function(){
	        						window.location.href= self_base_path + "/userCenter/personalData/personalData.html";
	        					}, 1000);
	        				}
	        			}else{ //绑定手机号
	        				window.location.href= self_base_path + "/userCenter/paymentPassword/signIn.html";
	        			}
	        		}else{ //微信授权
	        			var url = window.location;
	        			window.location.href= wxUrl + "/v3.2/login/auth?token=" + token + "&url=" + url;
	        		}
	        	}else{
	        		promptBox(data.msg);
	        	}
	        }else{
	        	promptBox('网络异常，请检查您的网络！');
	        }
		}
	);
}

/**
	url表示地址
**/
function pushVHistory(backUrl){
	if(backUrl == false){
		return;
	}
	var vhistory;
	var item = sessionStorage.getItem("_syw_dz_wx_vhistory_");
	if(item == null || item=="" || item=="undefined"){
		vhistory = [self_base_path + '/bigSupply/index.html'];
	}else{
		vhistory = JSON.parse(item);
	}
	var url;
	if(backUrl==null || backUrl==''){
		url = window.location.href;
	}else{
		url = backUrl;
	}
	
	url = url.replace(/auth=true/,'_nl=true');
	url = url.replace(/auth=false/,'_nl_=false');
	var lurl = vhistory.pop();
	if(url == lurl){
	}else{
		if(lurl != null)
			vhistory.push(lurl)
	}
	vhistory.push(url);
	console.log("push url："+ url);
	sessionStorage.setItem("_syw_dz_wx_vhistory_",JSON.stringify(vhistory));
	return;
}

/**
	-1表示历史记录后退一,go(-1);
	url表示地址
**/
function goBackVHistory(){
	var vhistory;
	var item = sessionStorage.getItem("_syw_dz_wx_vhistory_");
	if(item == null || item=="" || item=="undefined"){
		vhistory = [];
	}else{
		vhistory = JSON.parse(item);
	}
	/** 当前请求链接 **/
	var curl=window.location.href;
	
	var ctmp= window.location.pathname;
	
	var flag = true;
	var vhistoryUrl='';
	while(flag){
		vhistoryUrl=vhistory.pop();
		
		if(curl!=vhistoryUrl){
			flag = false;
		}
		if(flag == false && vhistoryUrl.split("?")[0].indexOf(ctmp)>0){
			flag = true;
		}
		if(vhistory.length<1){
			flag = false;
		}else{
			if(vhistoryUrl=='' || vhistoryUrl == null){
				flag = true;
			}
		}
	}
	sessionStorage.setItem("_syw_dz_wx_vhistory_",JSON.stringify(vhistory));
	if(vhistoryUrl!='' && vhistoryUrl != null){
		console.log("goBackVHistory url："+ vhistoryUrl);
		window.location.href=vhistoryUrl;
	}
	return;
}

/**
*pop出一个历史记录
**/
function popBackVHistory(){
	var vhistory;
	var item = sessionStorage.getItem("_syw_dz_wx_vhistory_");
	if(item == null || item=="" || item=="undefined"){
		vhistory = [];
	}else{
		vhistory = JSON.parse(item);
	}
	
	var vhistoryUrl=vhistory.pop();;
	sessionStorage.setItem("_syw_dz_wx_vhistory_",JSON.stringify(vhistory));
	if(vhistoryUrl!='' && vhistoryUrl != null){
		console.log("popBackVHistory url："+ vhistoryUrl);
	}
	return vhistoryUrl;
}

/*function goIndexPage(){
	setTimeout(function(){
		window.location.href=self_base_path + "/bigSupply/index.html";
	}, 1600);
}*/